package cn.jianyongjie.basic.controller;

import cn.jianyongjie.basic.dto.VerificationCode;
import cn.jianyongjie.basic.exception.CustomException;
import cn.jianyongjie.basic.service.IVerifyCodeService;
import cn.jianyongjie.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* @Description: TODO
* @Author: JianYongJie
* @Version: 1.0
* @Date:  2022/5/7 17:16
*/
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService iVerifyCodeService;


    /*
    * @Title: VerifyCodeController
    * @Description: 获取图形验证码
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/8 14:05
    * @Parameters: * @param key
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @GetMapping("/image/{key}")
    public AjaxResult image(@PathVariable("key") String key){

        try {

            String code = iVerifyCodeService.getVerificationCode(key);

            // 返回Ajax，以及图形验证码的base64位编码
            return AjaxResult.success().setResultObj(code);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }

    }

    /*
    * @Title: VerifyCodeController
    * @Description: 发送验证码接口
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/8 14:11
    * @Parameters: * @param verificationCode
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody VerificationCode code){

        try {

            iVerifyCodeService.sendSmsCode(code);

            // 返回Ajax
            return AjaxResult.success();

        }catch (CustomException e){
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }

    }
}
